#!/bin/sh 

######################################################################## 
# Start/Stop All Script for Domino on xLinux/zLinux/AIX/Solaris
# 2015-2016 Copyright by Daniel Nashed, feedback domino_unix@nashcom.de
# You may use and distribute the unmodified version of this script.
# Use at your own risk. No implied or specific warranties are given.
# You may change it for your own usage only
# Version 3.1.0 20.01.2016
########################################################################

# This script is intended to be a central interface ("super script") 
# to start/stop/etc  # multiple server partitions. 
# you have to customize the SCRIPT_NAME_PATTERN to match your Domino 
# partition start script entries.
# Thee script searches /etc/init.d for start script entries.
# In case you moved the script entries (e.g. when using systemd"),
# you have to change SCRIPT_LOCATION.

CURRENT_SCRIPT=`basename $0`
SCRIPT_NAME_PATTERN="rc_domino*"
SCRIPT_LOCATION=/etc/init.d
DOMINO_SHUTDOWN_TIMEOUT="300"

PARAM1=$1
PARAM2=$2
PARAM3=$3
PARAM4=$4

DOMINO_SERVERS=`find $SCRIPT_LOCATION/ -maxdepth 1 -name "$SCRIPT_NAME_PATTERN"`

all_servers_command ()
{
  for script_name in $DOMINO_SERVERS; do
   if [ ! "`basename $script_name`" = "$CURRENT_SCRIPT" ]; then
      #echo "Running " $script_name "$1" "$2"
      $script_name "$1" "$2"
      echo
    fi
  done

  return 0
}

start_all_servers ()
{
  # start all servers
  for script_name in $DOMINO_SERVERS; do
   if [ ! "`basename $script_name`" = "$CURRENT_SCRIPT" ]; then
     $script_name start
   fi
  done
 
  return 0
}

any_server_is_running ()
{
  DOMINO_RUNNING=`ps -ef | grep "/notes/latest/" | grep "server"`

  if [ -n "$DOMINO_RUNNING" ]; then
    # echo "Domino is running"
    return 1
  else
    # echo "Domino is NOT running"
    return 0
  fi

  return 0
}

wait_for_all_server_shutdown ()
{
  any_server_is_running
  if [ $? -eq 0 ]; then
    echo
    echo "All servers are stopped"
    echo
    return 0
  fi

  echo " ... Waiting for shutdown of all servers"

  count=0
  any_server_is_running
  while [ $? -eq 1 ]; do
    sleep 10
    count=`expr $count + 1`
    seconds=`expr $count "*" 10`
    echo " ... waiting $seconds seconds"
    if [ "$seconds" -ge "$DOMINO_SHUTDOWN_TIMEOUT" ]; then
      echo
      echo "Not all servers terminated yet"
      echo
      return 1
    fi

    any_server_is_running

  done

  return 0
}

wait_for_all_server_shutdown () 
{ 
  any_server_is_running 
  if [ $? -eq 0 ]; then 
    echo
    echo "All servers are already stopped" 
    echo
    return 0 
  fi 

  echo " ... Waiting for shutdown of all servers" 

  count=0 
  any_server_is_running 
  while [ $? -eq 1 ]; do 
    sleep 10 
    count=`expr $count + 1` 
    seconds=`expr $count "*" 10` 
    echo " ... waiting $seconds seconds" 
    if [ "$seconds" -ge "$DOMINO_SHUTDOWN_TIMEOUT" ]; then 
      echo
      echo "Not all servers terminated yet" 
      echo
      return 1 
    fi 

    any_server_is_running 

  done 

  echo
  echo "All servers are sucessfully stopped" 
  echo

  return 0 
}

stop_all_servers () 
{ 
  # stop all servers 
  for script_name in $DOMINO_SERVERS; do 
   if [ ! "`basename $script_name`" = "$CURRENT_SCRIPT" ]; then 
      $script_name stop > /dev/null 2>&1 & 
    fi 
  done 
  
  wait_for_all_server_shutdown 

  any_server_is_running 
  if [ $? -eq 1 ]; then 
    echo "Cleanup servers because of shutdown hang" 
    all_servers_command cleanup 
    return 0 
  fi 
  
  return 0 
} 


usage ()
{
  echo
  echo "Usage: `basename $0` { start | stop | status | statusall | clearlog | resources | cleanup | service}"
  echo

  return 0
}

case "$PARAM1" in 

  start)
    start_all_servers    
    ;;

  stop)
    stop_all_servers    
    ;;

  archivelog)
    all_servers_command "$PARAM1" "$PARAM2"
    ;;

  clearlog)
    all_servers_command "$PARAM1" "$PARAM2"
    ;;

  cleanup)
    all_servers_command "$PARAM1" "$PARAM2"
    ;;
  
  resources)
    all_servers_command "$PARAM1" "$PARAM2" 
    ;;

  service)
    all_servers_command "$PARAM1" "$PARAM2"
    ;;

  check)
    all_servers_command "$PARAM1" "$PARAM2"
    ;;
     
  status)
    any_server_is_running
    if [ $? -eq 0 ]; then
      echo "All servers already stopped"
      exit 5
    else
      echo
      echo "At least one server is running"
      echo
      exit 1
    fi
    ;;

  statusall)
    all_servers_command status display
    ;;

  statusrc)
    all_servers_command status
    ;;

  *)
    usage
    exit 1
    ;;

esac

exit 0 

